using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using IcecreamView;
using System;
using Framework.Timer;
using Framework.Audio;

namespace View{

    public class GuidePanel : IcecreamView.IC_AbstractModule
    {
        [BindUIPath("BG/NextBtn")]
        private Button NextBtn;
        [BindUIPath("BG")]
        private Transform _bg;

        private Action callback;
        private GameObject guideObj;

        private TimeHandler _timeHandler;

        public override void OnInitView()
        {
            NextBtn.onClick.AddListener(OnNext);
        }

        private void OnNext()
        {
            AudioManager.Instance.PlaySound("System/ui_btn2");
            ViewConnector.CloseView();
            callback?.Invoke();
        }

        public override void OnOpenView(Dictionary<string,IC_ViewData> parameters)
        {
            AudioManager.Instance.PlaySound("System/btn_3");
            var data = parameters["msg"];
            var guideName = data.GetValue<string>();
            callback = data.GetValue<Action>(1);
            guideObj = Resources.Load<GameObject>($"View/Guide/{guideName}");
            GameObject.Instantiate(guideObj, _bg);
            NextBtn.gameObject.SetActive(false);
            _timeHandler?.Kill();
            _timeHandler = GameUpdateMgr.Instance.CreateTimer(2f, () => {
                NextBtn.gameObject.SetActive(true);
            });
        }

        public override void OnCloseView()
        {
            _timeHandler?.Kill();
        }
    }
}
